#!/bin/bash

qc=lova-qc
test=lova-test
pre=tls-pre
prod=tls-prod
qcenv=config-DQ
testenv=config-DQ
preenv=config-y3pre
prodenv=config-vspprod

if [[ $1 == "lova-pre" ]];then
if [[ $2 == "hotfix" ]];then
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   qcimagename=`kubectl get deployments $service -n $qc -o json --kubeconfig=/root/.kube/$qcenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   qcver=`kubectl get deployments $service -n $qc -o json --kubeconfig=/root/.kube/$qcenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${qcimagename}:${qcver} == ${preimagename}:${prever} ]];then
      #echo "${qcimagename}:${qcver} equal ${preimagename}:${prever}"
      sleep 0.1
   else
      echo "${qcimagename}:${qcver} not equal ${preimagename}:${prever}"
   fi
done
else
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   testimagename=`kubectl get deployments $service -n $test -o json --kubeconfig=/root/.kube/$testenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   testver=`kubectl get deployments $service -n $test -o json --kubeconfig=/root/.kube/$testenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${testimagename}:${qcver} == ${preimagename}:${prever} ]];then
      #echo "${testimagename}:${testver} equal ${preimagename}:${prever}"
      sleep 0.1
   else
      echo "${testimagename}:${testver} not equal ${preimagename}:${prever}"
   fi
done
fi
elif [[ $1 == "lova-prod" ]];then
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   prodimagename=`kubectl get deployments $service -n $prod -o json --kubeconfig=/root/.kube/$prodenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prodver=`kubectl get deployments $service -n $prod -o json --kubeconfig=/root/.kube/$prodenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${preimagename}:${prever} == ${prodimagename}:${prodver} ]];then
      #echo "${preimagename}:${prever} equal ${prodimagename}:${prodver}"
      sleep 0.1
   else
      echo "${preimagename}:${prever} not equal ${prodimagename}:${prodver}"
   fi
done
elif [[ $1 == "uat" ]];then
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   preimagename=`kubectl get deployments $service -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`   prever=`kubectl get deployments $service -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   uatimagename=`kubectl get deployments $service -n uat -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   uatver=`kubectl get deployments $service -n uat -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${preimagename}:${prever} == ${uatimagename}:${uatver} ]];then
      #echo "${preimagename}:${prever} equal ${uatimagename}:${uatver}"
      sleep 0.1
   else
      echo "${preimagename}:${prever} not equal ${uatimagename}:${uatver}"
   fi
done
fi
